﻿// --- Copyright (c) 2010 Steven Hartgers ---
// Copyright (C) 2010 Steven Hartgers All rights are reserved.
// Author contact: fiinix00/at\gmail.com
// ------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more detail
//  
// You should have received a copy of the GNU General Public License 
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using Hack.Extensions;

namespace Hack
{
    public class Choice
    {
        ConsoleColor color;
        char character;
        double light;

        public Choice(ConsoleColor color, char character)
        {
            this.color = color;
            this.character = character;
            this.light = GetLuminosity(color, character);
        }

        public ConsoleColor Color { get { return color; } }
        public char Character { get { return character; } }
        public double Luminosity { get { return light; } }

        static readonly IEnumerable<ConsoleColor> colors = new[] { ConsoleColor.DarkGray, ConsoleColor.Gray, ConsoleColor.White };
        static readonly IEnumerable<char> characters = 'a'.To('z').Concat('A'.To('Z')).Concat('0'.To('9')).Concat(")(*&^%$#@!`~'\";[]-_=+\\|:/?.>,<{}");
        public static Choice[] GenerateChoices()
        {
            return new[] { new Choice(ConsoleColor.Black, ' ') }
                .Concat(from character in characters
                        from color in colors
                        let choice = new Choice(color, character)
                        orderby choice.Luminosity
                        select choice).ToArray();
        }

        static readonly Font font = new Font("Courier New", 10, FontStyle.Bold);
        static readonly Brush darkGrayBrush = new SolidBrush(System.Drawing.Color.FromArgb(132, 130, 132));
        static readonly Brush grayBrush = new SolidBrush(System.Drawing.Color.FromArgb(198, 195, 198));
        static readonly Bitmap bmp = new Bitmap(100, 100);
        static double GetLuminosity(ConsoleColor color, char character)
        {
            Brush brush = null;
            switch (color)
            {
                case ConsoleColor.DarkGray:
                    brush = darkGrayBrush;
                    break;
                case ConsoleColor.Gray:
                    brush = grayBrush;
                    break;
                case ConsoleColor.White:
                    brush = Brushes.White;
                    break;
                default:
                    brush = Brushes.Black;
                    break;
            }
            var g = Graphics.FromImage(bmp);
            g.FillRectangle(Brushes.Black, 0, 0, bmp.Width, bmp.Height);
            g.DrawString(character.ToString(), font, brush, 0, 0);
            var size = g.MeasureString(character.ToString(), font);

            return bmp.GetLuminosity(new Rectangle(0, 0, (int)size.Width, (int)size.Height));
        }
    }
}